//
//  TravelDateViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2017/12/25.
//  Copyright © 2017年 Lwx. All rights reserved.
//
#define border 10
#define labelH 30
#define nextStepBtnH 50
#import "TravelDateViewController.h"
#import "XXTripTableViewController.h"
#import "SearchTravelViewController.h"
#import "ChooseJourneyViewController.h"
@interface TravelDateViewController ()
@property (nonatomic, strong)UILabel * travelDateLab;
@property (nonatomic, strong)UILabel * travelDaysLab;
@property (nonatomic, strong)UIDatePicker * datePicker;
@property (nonatomic, strong)UILabel * showLabel;
@property (nonatomic, assign)NSString * yearStr;
@property (nonatomic, assign)NSString * monthStr;
@property (nonatomic, assign)NSString * dayStr;
@property (nonatomic, strong)UIButton * yearBtn;
@property (nonatomic, strong)UIButton * monthBtn;
@property (nonatomic, strong)UIButton * dayBtn;
@property (nonatomic, strong)UIButton * oneDayBtn;
@property (nonatomic, strong)UIButton * twoDayBtn;
@property (nonatomic, strong)UIButton * threeDayBtn;

@property (nonatomic, assign)NSString * travelDayStr;
@property (nonatomic, assign)int travelDayInt;
@property (nonatomic, assign)int timeSign;//时间戳
@property (nonatomic, strong)NSString *DateTime;
@property (nonatomic, strong)NSString * trip_id;//行程ID
@end

@implementation TravelDateViewController(networking)
- (void)submitJourneyData{
    NSDictionary * dic = @{
                           @"start_time":@(self.timeSign),
                           @"city":self.city,
                           @"mid":[UserModel intance].member_id,
                           @"token":[UserModel intance].token,
                           @"day":@(self.travelDayInt)
                           };
    WS(ws)
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/index/addTrip") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        if (error) {
            [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
        }else{
            if ([obj[@"code"] integerValue] == 100000){
                ws.trip_id = obj[@"data"][@"trip_id"];
                ChooseJourneyViewController * vc = [[ChooseJourneyViewController alloc] init];
                vc.trip_id = ws.trip_id;
                vc.travelDay = ws.travelDayInt;
                vc.timeSign = ws.timeSign;
                vc.city = ws.city;
//                [[NSUserDefaults standardUserDefaults] setObject:@(ws.travelDayInt) forKey:@"selectTravelDays"];
//                [[NSUserDefaults standardUserDefaults] synchronize];
//                vc.selectTravelDays = ws.travelDayInt;
                [self.navigationController pushViewController:vc animated:YES];

            }else{
                [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
            }
        }
    }];
}


@end

@implementation TravelDateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"出行日期";
    
    _travelDateLab = [[UILabel alloc] initWithFrame:CGRectMake(border, 10, 150, labelH)];
    _travelDateLab.text = @"出行日期";
    _travelDateLab.font = DEF_FontSize_14;
    [self.view addSubview:_travelDateLab];
    
    _travelDaysLab = [[UILabel alloc] initWithFrame:CGRectMake(border, 150, 150, labelH)];
    _travelDaysLab.text = @"出行天数";
    _travelDaysLab.font = DEF_FontSize_14;
    [self.view addSubview:_travelDaysLab];
    
    [self initDatePickView];
    [self.view addSubview:self.datePicker];
    
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    self.DateTime = [formatter stringFromDate:date];
    
    //NSDate格式转换为NSString格式
    NSDate *pickerDate = [self.datePicker date];// 获取用户通过UIDatePicker设置的日期和时间
    NSString *dateString = [formatter stringFromDate:pickerDate];
    
    NSString *timeStr = [formatter stringFromDate:pickerDate];//----------将nsdate按formatter格式转成nsstring
    NSDate *dateNow2 = [formatter dateFromString:timeStr];
    NSInteger timeTeger = [[NSNumber numberWithDouble:[dateNow2 timeIntervalSince1970]] integerValue];
//    NSLog(@"时间戳%li", (long)timeTeger);
    self.timeSign = (int)timeTeger;
    
    
    self.oneDayBtn = (UIButton *)[self.view viewWithTag:2000];
    self.twoDayBtn = (UIButton *)[self.view viewWithTag:2001];
    self.threeDayBtn = (UIButton *)[self.view viewWithTag:2002];
    
    UIButton * nextStepBtn = [[UIButton alloc] init];
    nextStepBtn.frame = CGRectMake(0, SCREEN_HEIGHT - nextStepBtnH - 64, SCREEN_WIDTH, nextStepBtnH);
    [nextStepBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextStepBtn setTitle:@"下一步" forState:UIControlStateNormal];
    nextStepBtn.backgroundColor = [UIColor themeColor];
    nextStepBtn.titleLabel.font = DEF_FontSize_14;
    [nextStepBtn addTarget:self action:@selector(nextStepBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextStepBtn];
    
    [self getDate:self.DateTime];
}

- (void)getDate:(NSString *)specificTimeStr{
    
    _yearStr = [specificTimeStr substringWithRange:NSMakeRange(0,4)];
    _monthStr = [specificTimeStr substringWithRange:NSMakeRange(5,2)];
    _dayStr = [specificTimeStr substringWithRange:NSMakeRange(8,2)];
    self.yearBtn = (UIButton *)[self.view viewWithTag:1000];
    [self.yearBtn setTitle:_yearStr forState:UIControlStateNormal];
    
    self.monthBtn = (UIButton *)[self.view viewWithTag:1001];
    [self.monthBtn setTitle:_monthStr forState:UIControlStateNormal];
    
    self.dayBtn = (UIButton *)[self.view viewWithTag:1002];
    [self.dayBtn setTitle:_dayStr forState:UIControlStateNormal];
}

- (void)initDatePickView{
    int dateBtnW = 60;
    int dateBtnX = SCREEN_WIDTH / 6;
    int labelW = 15;
    int travelDaysBtnW = 80;
    int travelDaysBtnX = (SCREEN_WIDTH - 3 * travelDaysBtnW) / 4;
    int travelDaysBtnH = 40;
    for (int i = 0; i<3; i++) {
        UIButton * dateBtn = [[UIButton alloc] init];
        dateBtn.tag = i + 1000;
        dateBtn.layer.borderColor = getColor(@"ffbb02").CGColor;
        dateBtn.layer.borderWidth = 1;
        [dateBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [dateBtn setTitle:@"2017" forState:UIControlStateNormal];
        [dateBtn setCornerRadius:5];
        dateBtn.frame = CGRectMake(dateBtnX +( dateBtnW + labelW )*i, self.travelDateLab.bottom + border, dateBtnW, 25);
        dateBtn.adjustsImageWhenDisabled = NO;
        [dateBtn addTarget:self action:@selector(datePickerShow) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:dateBtn];
        
        UILabel * dateTitleLab = [[UILabel alloc] init];
        dateTitleLab.font = DEF_FontSize_12;
        dateTitleLab.frame = CGRectMake(dateBtn.right + 2, dateBtn.top, labelW, dateBtn.height);
        
        UIButton * travelDaysBtn = [[UIButton alloc] init];
        [travelDaysBtn setBackgroundColor:[UIColor whiteColor]];
        [travelDaysBtn setTitleColor:getColor(@"ffbb02") forState:UIControlStateNormal];
        [travelDaysBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        travelDaysBtn.layer.borderColor = getColor(@"ffbb02").CGColor;
        travelDaysBtn.layer.borderWidth = 1;
        travelDaysBtn.adjustsImageWhenDisabled = NO;
        [travelDaysBtn setCornerRadius:10];
        travelDaysBtn.frame = CGRectMake(travelDaysBtnX + (travelDaysBtnW +travelDaysBtnX) * i, self.travelDaysLab.bottom + border, travelDaysBtnW, travelDaysBtnH);
        travelDaysBtn.tag = i + 2000;
        [self.view addSubview:travelDaysBtn];
        
        if (i == 0) {
            dateTitleLab.text = @"年";
            [travelDaysBtn setTitle:@"1天" forState:UIControlStateNormal];
            [travelDaysBtn addTarget:self action:@selector(oneDayBtnClick) forControlEvents:UIControlEventTouchUpInside];
        }else if (i == 1){
            dateTitleLab.text = @"月";
            [travelDaysBtn setTitle:@"2天" forState:UIControlStateNormal];
            [travelDaysBtn addTarget:self action:@selector(twoDayBtnClick) forControlEvents:UIControlEventTouchUpInside];

        }else{
            dateTitleLab.text = @"日";
            [travelDaysBtn setTitle:@"3天" forState:UIControlStateNormal];
            [travelDaysBtn addTarget:self action:@selector(threeDayBtnClick) forControlEvents:UIControlEventTouchUpInside];

        }
        [self.view addSubview:dateTitleLab];
    }
}

- (UIDatePicker *)datePicker{
    if (!_datePicker) {
        int datePickerH = 100;
        _datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - datePickerH - 64 - nextStepBtnH, self.view.frame.size.width, datePickerH)];
        _datePicker.datePickerMode = UIDatePickerModeDate;
        [_datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged ];
        _datePicker.hidden = YES;
        //设置显示格式
        //默认根据手机本地设置显示为中文还是其他语言
        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中文显示
        self.datePicker.locale = locale;
        
        //当前时间创建NSDate
        NSDate *localDate = [NSDate date];
        NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
        //设置时间
        [offsetComponents setYear:0];
        [offsetComponents setMonth:0];
        [offsetComponents setDay:5];
        [offsetComponents setHour:20];
        [offsetComponents setMinute:0];
        [offsetComponents setSecond:0];
        //设置最大值时间
//        NSDate *maxDate = [gregorian dateByAddingComponents:offsetComponents toDate:localDate options:0];
        //设置属性
        self.datePicker.minimumDate = localDate;
//        self.datePicker.maximumDate = maxDate;
        
    }return _datePicker;
}

-(void)dateChanged:(id)sender{
    UIDatePicker *control = (UIDatePicker*)sender;
    NSDate* date = control.date;
    //添加你自己响应代码
    NSLog(@"dateChanged响应事件：%@",date);
    //NSDate格式转换为NSString格式
    NSDate *pickerDate = [self.datePicker date];// 获取用户通过UIDatePicker设置的日期和时间
    NSDateFormatter *pickerFormatter = [[NSDateFormatter alloc] init];// 创建一个日期格式器
    [pickerFormatter setDateStyle:NSDateFormatterMediumStyle];
    [pickerFormatter setTimeStyle:NSDateFormatterShortStyle];
    [pickerFormatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *dateString = [pickerFormatter stringFromDate:pickerDate];
    
    NSString *timeStr = [pickerFormatter stringFromDate:pickerDate];//----------将nsdate按formatter格式转成nsstring
    NSDate *dateNow2 = [pickerFormatter dateFromString:timeStr];
    NSInteger timeTeger = [[NSNumber numberWithDouble:[dateNow2 timeIntervalSince1970]] integerValue];
//    NSLog(@"时间戳%li", (long)timeTeger);
    self.timeSign = timeTeger;
    //打印显示日期时间
//    NSLog(@"格式化显示时间：%i",timeTeger);
    _showLabel.text = dateString;
    [self getDate:dateString];
}

- (void)oneDayBtnClick{
    self.oneDayBtn.selected = !self.oneDayBtn.selected;
    if (self.oneDayBtn.selected) {
        [self.oneDayBtn setBackgroundColor:getColor(@"ffbb02")];
        self.travelDayStr = [self.oneDayBtn.titleLabel.text substringWithRange:NSMakeRange(0,1)];
        self.travelDayInt = [self.travelDayStr intValue];
    }else{
        [self.oneDayBtn setBackgroundColor:[UIColor whiteColor]];
        self.travelDayStr = nil;
        self.travelDayInt = [self.travelDayStr intValue];
    }
    if (self.oneDayBtn.selected) {
        self.twoDayBtn.selected = NO;
        self.threeDayBtn.selected = NO;
        [self.twoDayBtn setBackgroundColor:[UIColor whiteColor]];
        [self.threeDayBtn setBackgroundColor:[UIColor whiteColor]];
        
    }
    
}

- (void)twoDayBtnClick{
    self.twoDayBtn.selected = !self.twoDayBtn.selected;
    if (self.twoDayBtn.selected) {
        [self.twoDayBtn setBackgroundColor:getColor(@"ffbb02")];
        self.travelDayStr = [self.twoDayBtn.titleLabel.text substringWithRange:NSMakeRange(0,1)];
        self.travelDayInt = [self.travelDayStr intValue];
    }else{
        [self.twoDayBtn setBackgroundColor:[UIColor whiteColor]];
        self.travelDayStr = nil;
        self.travelDayInt = [self.travelDayStr intValue];
    }
    if (self.twoDayBtn.selected) {
        self.oneDayBtn.selected = NO;
        self.threeDayBtn.selected = NO;
        [self.oneDayBtn setBackgroundColor:[UIColor whiteColor]];
        [self.threeDayBtn setBackgroundColor:[UIColor whiteColor]];
    }
    
}

- (void)threeDayBtnClick{
    self.threeDayBtn.selected = !self.threeDayBtn.selected;
    if (self.threeDayBtn.selected) {
        [self.threeDayBtn setBackgroundColor:getColor(@"ffbb02")];
        self.travelDayStr = [self.threeDayBtn.titleLabel.text substringWithRange:NSMakeRange(0,1)];
        self.travelDayInt = [self.travelDayStr intValue];
    }else{
        [self.threeDayBtn setBackgroundColor:[UIColor whiteColor]];
        self.travelDayStr = nil;
        self.travelDayInt = [self.travelDayStr intValue];
    }
    if (self.threeDayBtn.selected) {
        self.oneDayBtn.selected = NO;
        self.twoDayBtn.selected = NO;
        [self.oneDayBtn setBackgroundColor:[UIColor whiteColor]];
        [self.twoDayBtn setBackgroundColor:[UIColor whiteColor]];
    }
}

- (void)datePickerShow{
    self.datePicker.hidden = NO;
//    [self getDate:self.DateTime];
}

- (void)nextStepBtnClick{
    if (self.DateTime.length == 0 || self.travelDayStr.length == 0) {
        [[HUDHelper sharedInstance] tipMessage:@"请选择出行日期和出行天数"];
        return;
    }
    if ([UserModel isSaveLogin]) {
        [self submitJourneyData];
    }else{
        [AppHelper showLogin];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
